using System;
using System.Collections.Generic;
using Comdiv.Extensions;
using Comdiv.Model;
using Comdiv.Model.Interfaces;
using Comdiv.Logging;
using Comdiv.Application;
using Comdiv.Persistence;
using Comdiv.Inversion;
using Comdiv.Security.Acl;
using Comdiv.Conversations;
using Comdiv.IO;
using Comdiv.Security;
using System.Linq;

namespace Comdiv.Messaging{
    /// <summary>
    /// to avoid lazy-instantiation to access native fields of collections
    /// </summary>
    internal interface IMessageHack{
        IList<IMessageRead> HackedReads { get; set; }
        IList<IMessageClose> HackedArchives { get; set; }
    }
    /// <summary>
    /// Default POCO implementation of IMessage
    /// </summary>
    public class Message : IMessage,IMessageHack {
        public Message(){
            this.Type = MessageTypes.Default;
            this.Sender = myapp.usrName;
            this.Active = true;
            this.CanBeArchived = true;
            this.AnswerDate = DateExtensions.Begin;
        }

        public virtual DateTime AnswerDate
        {
            get; set;
        }

        public virtual string GetWiki(){
            return Text.replace(@"\[([\s\S]+?)\|([\s\S]+?)\]", m =>{
                                                                   var text = m.Groups[1].Value;
                                                                   var url = m.Groups[2].Value;
                                                                   return "<a href='" + url.Trim().Replace("&", "&amp;") +
                                                                          "' target='_blank'>" + text + "</a>";
                                                               });
        }
        public virtual int Id { get; set; }
        public virtual string Code { get; set; }
        public virtual DateTime Version { get; set; }
        public virtual string Type { get; set; }
        public virtual string Sender { get; set; }
        public virtual string Target { get; set; }
        public virtual string Text { get; set; }
        public virtual string Answer { get; set; }
        private IList<IMessageRead> _reads;
        public virtual IList<IMessageRead> Reads{
            get { return _reads; }
            set { _reads = value; }
        }

        private IList<IMessageClose> _archives;
        public virtual IList<IMessageClose> Archives{
            get { return _archives; }
            set { _archives = value; }
        }

        public virtual bool Active { get; set; }
        public virtual bool CanBeArchived { get; set; }
        public virtual IList<IMessageRead> HackedReads{
            get { return _reads; }
            set { _reads = value; }
        }

        public virtual IList<IMessageClose> HackedArchives{
            get { return _archives;}
            set { _archives = value; }
        }
    }
}